package cn.gengar.domain.activity.service.trial.thread;

import cn.gengar.domain.activity.adapter.repository.IActivityRepository;
import cn.gengar.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import cn.gengar.domain.activity.model.valobj.SCSkuActivityVO;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;
import java.util.concurrent.Callable;

public class QueryGroupBuyActivityDiscountVOThreadTask implements Callable<GroupBuyActivityDiscountVO> {

    private final String source;
    private final String channel;
    private final String goodsId;
    private final IActivityRepository activityRepository;

    public QueryGroupBuyActivityDiscountVOThreadTask(String source, String channel, String goodsId, IActivityRepository activityRepository) {
        this.source = source;
        this.channel = channel;
        this.goodsId = goodsId;
        this.activityRepository = activityRepository;
    }

    @Override
    public GroupBuyActivityDiscountVO call() throws Exception {
        if (StringUtils.isAllBlank(source, channel, goodsId)) {
            return GroupBuyActivityDiscountVO.builder().build();
        }
        // 查询活动id
        SCSkuActivityVO scSkuActivityVO = activityRepository.querySCSkuActivityBySCGoodsId(source, channel, goodsId);
        if (Objects.isNull(scSkuActivityVO) || Objects.isNull(scSkuActivityVO.getActivityId())) {
            return GroupBuyActivityDiscountVO.builder().build();
        }

        Long activityId = scSkuActivityVO.getActivityId();
        return activityRepository.queryGroupBuyActivityDiscountVO(activityId);
    }
}
